package com.heima.comment.service.impl;


import com.heima.apis.wemedia.SensitiveClient;

import com.heima.comment.service.CommentAutoScanService;
import com.heima.comment.service.CommentBaiduScanService;
import com.heima.common.exception.CustomException;
import com.heima.comment.pojos.ApArticleComment;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.utils.common.SensitiveWordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 陈辉
 * @data 2023 16:24
 */
@Service
public class CommentAutoScanServiceImpl  implements CommentAutoScanService {

    @Autowired
    private CommentBaiduScanService commentBaiduScanService;

    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private SensitiveClient sensitiveClient;

    /**
     * 评论自动审核功能
     *
     * @param id
     * @return
     */
    @Override
    public boolean autoScan(String id) {
        //1. 校验审核是否存在

        Query query = Query.query(Criteria.where("_id").is(id));
        ApArticleComment apArticleComment = mongoTemplate.findOne(query, ApArticleComment.class);


        if (apArticleComment == null) {
            throw new CustomException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //先走系统内部的敏感词过滤
        boolean result = handleSensitiveWord(apArticleComment.getContent());
        if (!result) {
            return result;
        }
        //再走外部的敏感词过滤
        Map<String, String> resultMap = commentBaiduScanService.scanText(apArticleComment.getContent());
        String conclusionType = resultMap.get("conclusionType");
        if ("1".equals(conclusionType)) {
            //文本审核通过
            result = true;
        }
        return result;
    }

    /**
     * 自定义敏感词过滤
     *
     * @param text   文本内容
     * @param text
     * @return
     */
    private boolean handleSensitiveWord(String text) {
        //1. 查询数据库表：wm_sensitive，得到所有的自管理敏感词

      /*  List<String> list = commentSensitiveMapper.selectList(null)
                .stream().map(WmSensitive::getSensitives)
                .collect(Collectors.toList());*/

        SensitiveDto sensitiveDto = new SensitiveDto();
        sensitiveDto.setName(null);

        ResponseResult responseResult = sensitiveClient.list(sensitiveDto);
        List<String> list = new ArrayList<>();
        list.add(responseResult.toString());

        //2. 初始化DFA算法的敏感词库
        SensitiveWordUtil.initMap(list);

        //3. 敏感词匹配
        Map<String, Integer> map = SensitiveWordUtil.matchWords(text);

        //4. 处理结果
        if (map == null || map.isEmpty()) {
            //没有敏感词，返回true
            return true;
        } else {
            //修改文章状态：2（审核失败）
            return false;
        }
    }
}